<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box {
            position: absolute;
            top: 100px;
            left: 100px;
            width: 100px;
            height: 100px;
            background-color: gold;
        }
    </style>
</head>

<body>
    <button id="btn">
        开始运动
    </button>
    <div id="box">

    </div>
    <script>
        var box = document.getElementById('box')
        var btn = document.getElementById("btn");
        //设置全局变量的left的值
        var left = 100;
        //按键监听
        btn.onclick = (function() {
            setInterval(function() {
                left += 2;
                box.style.left = left + 'px';
            }, 20)
        })
    </script>
</body>

</html>